<template>
  <div class="ele-body">
    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="drctId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()" v-permission="['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_ADD_BUTON']">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建配置模板</span>
              </a-button>
            </a-space>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)" v-permission="['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_EDIT_BUTON']">修改</a>
                <a-divider type="vertical" v-any-permission-and="[['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_EDIT_BUTON'], ['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_DEL_BUTON', 'PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_DISTRIBUTE_BUTON']]" />
                <div v-permission="['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_DEL_BUTON']">
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </div>
                <a-divider type="vertical" v-any-permission-and="[['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_EDIT_BUTON', 'PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_DEL_BUTON'], ['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_DISTRIBUTE_BUTON']]"  />
                <a @click="distributeTemplate(record)" v-permission="['PROPERTY_DEVICELIST_ROLLERSHADES_TEMPLET_CONIG_DISTRIBUTE_BUTON']">分发</a>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <DeviceRollershadesFirmwareRollershadesConfigTemplateEdit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    <DeviceRollershadesFirmwareRollershadesConfigTemplateDistribute v-if="showDistribute" v-model:visible="showDistribute" :data="current" @done="reload" />
  </div>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import DeviceRollershadesFirmwareRollershadesConfigTemplateEdit from './rollershades-firmware-rollershades-config-template-edit.vue';
import DeviceRollershadesFirmwareRollershadesConfigTemplateDistribute from './rollershades-firmware-rollershades-config-template-distribute.vue'
import {DeviceRollershadesConfigTemplateApi} from "@/api/property/rollershades/DeviceRollershadesConfigTemplateApi";
export default {
  name: 'DeviceRollershadesFirmwareRollershadesConfigTemplate',
  components: {
    PlusOutlined,
    DeviceRollershadesFirmwareRollershadesConfigTemplateEdit,
    DeviceRollershadesFirmwareRollershadesConfigTemplateDistribute
  },
  async mounted() {
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '模板id',
          dataIndex: 'drctId'
        },
        {
          title: '模板名称',
          dataIndex: 'templateName'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showEdit3 : false,
      showDistribute : false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    async remove(row) {
      const result = await DeviceRollershadesConfigTemplateApi.delete({ drctId: row.drctId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },


    distributeTemplate(row) {
      this.current = row;
      this.showDistribute = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceRollershadesConfigTemplateApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
